/**
 * Copyright (c) Tapas Mobile.  All Rights Reserved.
 *
 * @author DaiHui 
 * @version 1.0
 */

package com.dotools.utils;

import android.text.TextUtils;
import android.text.format.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.UUID;

public class TimeUtils {

    public static String getCurentTimeString() {
        return TimeUtils.getTimeString(System.currentTimeMillis());
    }

    public static String getTimeString(long time) {
        Time now = new Time();
        now.set(time);
        StringBuilder sb = new StringBuilder();
        sb.append(now.year).append("-").append((now.month + 1)).append("-").append(now.monthDay).append("_")
                .append(now.hour).append("-").append(now.minute).append("-").append(now.second);
        return sb.toString();
    }

    
    public static final String DATE = "yyyy-MM-dd";
    public static final String DATETIME = "yyyy-MM-dd HH:mm:ss";
    public static final String DATETIMEHOUR = "yyyy-MM-dd HH";

    public static SimpleDateFormat simpleTimeFormat2Date = new SimpleDateFormat(DATE);
    public static SimpleDateFormat simpleTimeFormat2hour = new SimpleDateFormat(DATETIMEHOUR);
    public static SimpleDateFormat simpleTimeFormat2second = new SimpleDateFormat(DATETIME);

    public static final  SimpleDateFormat simpleTimeFormatShort24 = new SimpleDateFormat("HH:mm");
    public static final  SimpleDateFormat simpleTimeFormatShort12 = new SimpleDateFormat("hh:mm");
    public static final  SimpleDateFormat simpleDateFormatMDDate = new SimpleDateFormat("MM.dd");
    
    public static TimeZone sTimeZone = TimeZone.getDefault();
    static {
        initTimeZone();
    }

    public static void initTimeZone(){
        sTimeZone = TimeZone.getDefault();
        simpleTimeFormat2Date.setTimeZone(sTimeZone);
        simpleTimeFormat2hour.setTimeZone(sTimeZone);
        simpleTimeFormat2second.setTimeZone(sTimeZone);
        simpleTimeFormatShort24.setTimeZone(sTimeZone);
        simpleTimeFormatShort12.setTimeZone(sTimeZone);
        simpleDateFormatMDDate.setTimeZone(sTimeZone);
    }


    
    public static Calendar getCalenderTZ(){
        return Calendar.getInstance(sTimeZone);
    }

    public static String formatDate1(Date date,String pattern) {
        if(date == null) {
            date = getCalenderTZ().getTime();
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }
    
    public static String formatDate(Date date) {
        if(date == null) {
            date = getCalenderTZ().getTime();
        }
        return simpleTimeFormat2Date.format(date);
    }

    public static String formatSecond(Date date) {
        if(date == null) {
            date = getCalenderTZ().getTime();;
        }
        return simpleTimeFormat2second.format(date);
    }

    public static String formatHour(Date date) {
        if(date == null) {
            date = getCalenderTZ().getTime();;
        }
        return simpleTimeFormat2hour.format(date);
    }
 

    public static Date parseDate(String dateStr){
        return parseDate(dateStr,DATE);
    }

    public static Date parseTime(String dateStr){
        return parseDate(dateStr,DATETIME);
    }

    public static Date parseDate(String dateStr,String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }

    public static String converTime(long timestamp) {
        long currentSeconds = System.currentTimeMillis() / 1000;
        long timeGap = currentSeconds - timestamp;// 与现在时间相差秒数
        String timeStr = null;
        if (timeGap > 30 * 24 * 60 * 60) {
            timeStr = timeGap / (30 * 24 * 60 * 60) + "个月前";
        } else if (timeGap >= 7 * 24 * 60 * 60) {
            timeStr = timeGap / (7 * 24 * 60 * 60) + "周前";
        } else if (timeGap > 24 * 60 * 60 && timeGap < 7 * 24 * 60 * 60) {// 1天以上
            long day = Long.parseLong(new SimpleDateFormat("dd").format(System.currentTimeMillis())) - Long.parseLong(new SimpleDateFormat("dd").format(timestamp * 1000));
            if (day <= 0) {
                Date d1 = new Date(System.currentTimeMillis());
                Date d2 = new Date(timestamp * 1000);
                SimpleDateFormat s1 = new SimpleDateFormat("MM-dd");
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                s1.format(d1);
                s1.format(d2);
                Date date = new Date(timestamp * 1000);
                if (d1.compareTo(d2) >= 1) {
                    timeStr = timeGap / (24 * 60 * 60) + 1 + "天前" + sdf.format(date);
                } else {
                    timeStr = sdf.format(date);
                }
            } else if (day == 1) {
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                Date date = new Date(timestamp * 1000);
                timeStr = "昨天 " + sdf.format(date);
            } else {
                timeStr = timeGap / (24 * 60 * 60) + "天前";
            }
        } else if (timeGap > 60 * 60) {// 1小时-24小时
            long day = Long.parseLong(new SimpleDateFormat("dd").format(System.currentTimeMillis())) - Long.parseLong(new SimpleDateFormat("dd").format(timestamp * 1000));
            if (day <= 0) {
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                Date date = new Date(timestamp * 1000);
                timeStr = sdf.format(date);
            } else if (day == 1) {
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                Date date = new Date(timestamp * 1000);
                timeStr = "昨天 " + sdf.format(date);
            } else {
                timeStr = timeGap / (24 * 60 * 60) + "天前";
            }
        } else if (timeGap > 60) {// 1分钟-59分钟
            timeStr = timeGap / 60 + "分钟前";
        } else if (timeGap < 59 && timeGap > 10) {// 10秒钟-59秒钟
            timeStr = timeGap + "秒前";
        } else if (timeGap < 10) {
            timeStr = "刚刚";
        }
        return timeStr;
    }

    /**
     * 通过时间戳获取天数
     *
     * @param time
     * @return
     */
    public static int getDayNumber(int time) {
        return (time / (24 * 60 * 60)) - 1;
    }

    public static long getSubDays(long old, long now) {

        long nowDays  =  now%(60*60*24) ==0 ?(now/(60*60*24)):(now/(60*60*24)+1)  ;
        long oldDays = old%(60*60*24) ==0?(old/(60*60*24)):(old/(60*60*24)+1) ;
        long avg = nowDays - oldDays+1;

        return avg ;
    }

    /**
     * 两个日期相差的天数
     *
     * @param nextDate
     * @param preDate
     * @return
     */
    public static int getDayNumber(Date nextDate, Date preDate) {
        return (int) ((nextDate.getTime() - preDate.getTime()) / (1000l * 60l * 60l * 24l));
    }

    public static String getDate(String sTime) {
        long timeLong = Long.parseLong(sTime);
        Date date = new Date(timeLong * 1000);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");

        String time[] = sdf.format(date).split("/");
        String month = time[0];
        String day = time[1];

        return StringUtil.reMoveLeft(month) + "/" + StringUtil.reMoveLeft(day);
    }

    public static String NearFriendPageConverTime(long timestamp) {
        long currentSeconds = System.currentTimeMillis() / 1000;
        long timeGap = currentSeconds - timestamp;// 与现在时间相差秒数
        String timeStr = "刚刚";
        if (timeGap > 30 * 24 * 60 * 60) {
            timeStr = timeGap / (30 * 24 * 60 * 60) + "月前";
        } else if (timeGap > 7 * 24 * 60 * 60) {
            timeStr = timeGap / (7 * 24 * 60 * 60) + "周前";
        } else if (timeGap > 24 * 60 * 60) {// 1天以上
            timeStr = timeGap / (24 * 60 * 60) + "天前";
        } else if (timeGap > 60 * 60) {// 1小时-24小时
            timeStr = timeGap / (60 * 60) + "小时前";
        } else if (timeGap > 60) {// 1分钟-59分钟
            timeStr = timeGap / 60 + "分钟前";
        } else if (timeGap < 59 && timeGap > 10) {// 10秒钟-59秒钟
            timeStr = timeGap + "秒前";
        } else if (timeGap < 10) {
            timeStr = "刚刚";
        }
        return timeStr;
    }

    public static String getStandardTime(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm");
        Date date = new Date(timestamp * 1000);
        sdf.format(date);
        return sdf.format(date);
    }

    public static String getHHmm(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        Date date = new Date(timestamp * 1000);
        sdf.format(date);
        return sdf.format(date);
    }

    /**
     * 通过时间戳获得星期
     *
     * @param timestamp
     * @return
     */
    public static String getWeekTime(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 EEEE");
        Date date = new Date(timestamp * 1000);
        String week = sdf.format(date);
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return weekDays[w];
    }

    /**
     * 通过时间戳获得月日
     *
     * @param timestamp
     * @return
     */
    public static String getDateTime(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
        Date date = new Date(timestamp * 1000);
        return sdf.format(date);
    }

    /**
     * 通过时间戳获得年月日
     *
     * @param timestamp
     * @return
     */
    public static String getYearTime(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = new Date(timestamp * 1000);
        return sdf.format(date);
    }

    /**
     * 通过时间戳获得年
     *
     * @param timestamp
     * @return
     */
    public static String getYearByTime(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date(timestamp * 1000);
        return sdf.format(date);
    }

    /**
     * 通过时间戳获得月
     *
     * @param timestamp
     * @return
     */
    public static String getMonthTime(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        Date date = new Date(timestamp * 1000);
        return sdf.format(date);
    }

    /**
     * 通过时间戳获得日
     *
     * @param timestamp
     * @return
     */
    public static String getDayTime(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        Date date = new Date(timestamp * 1000);
        return sdf.format(date);
    }

    /**
     * 通过时间戳获得星期
     *
     * @param timestamp
     * @return
     */
    public static String getWeekTimeByList(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        Date date = new Date(timestamp * 1000);
        return sdf.format(date);
    }

    /**
     * 通过时间戳获得年月日及时间 格式：*年*月*日，**：**
     *
     * @param
     * @return
     */
    public static String getYMDHM(long timestamap) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date(timestamap * 1000);
        String year = sdf.format(date);
        sdf = new SimpleDateFormat("MM");
        date = new Date(timestamap * 1000);
        String month = sdf.format(date);
        sdf = new SimpleDateFormat("dd");
        date = new Date(timestamap * 1000);
        String day = sdf.format(date);
        sdf = new SimpleDateFormat("HH:mm");
        date = new Date(timestamap * 1000);
        String time = sdf.format(date);

        String times = year + "年" + month + "月" + day + "日" + "," + time;

        return times;
    }

    public static String getyyyyMMddHHmmss(Date date) {
        if (null != date) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sdf.setTimeZone(TimeZone.getTimeZone("Asia/Hong_Kong"));
            return sdf.format(date);
        }
        return "";
    }

    public static String getyyyyMMddHHmmss2(Timestamp date) {
        if (null != date) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sdf.setTimeZone(TimeZone.getTimeZone("Asia/Hong_Kong"));
            return sdf.format(date);
        }
        return "";
    }

    /** 是否是昨天 */
    public static boolean isYestoday(long time) {
        boolean flag = false;
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        String yday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        String today = getyyyyMMdd(getDateByTime(time));
        return flag;
    }

    public static String getyyyyMMdd(Date date) {
        if (null != date) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sdf.setTimeZone(TimeZone.getTimeZone("Asia/Hong_Kong"));
            return sdf.format(date);
        }
        return "";
    }

    /** 时间戳转DATE */
    public static Date getDateByTime(long unixDate) {
        SimpleDateFormat fm1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        SimpleDateFormat fm2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        long unixLong = 0;
        String date = "";
        try {
            unixLong = unixDate * 1000;
        } catch (Exception ex) {
            System.out.println("String转换Long错误，请确认数据可以转换！");
        }
        try {
            date = fm2.format(unixLong);
            // date = fm2.format(new Date(date));
        } catch (Exception ex) {
            System.out.println("String转换Date错误，请确认数据可以转换！");
        }
        return new Date(date);
    }

    public static String getyyyyMMddHHmmssSSS(Date date) {
        if (null != date) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            sdf.setTimeZone(TimeZone.getTimeZone("Asia/Hong_Kong"));
            return sdf.format(date);
        }
        return "";
    }

    public static String getyyyyMMddHHmmssSSS() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Hong_Kong"));
        return sdf.format(getChinaDate());
    }

    public static Date getChinaDate() {
        // 用中国时区、中国本地语言环境创建时间
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Asia/Hong_Kong"), Locale.CHINESE);
        return cal.getTime();
    }

    public static Date getChinaDate(int year, int month, int date, int hour, int minute, int second, int millisecond) {
        // 用中国时区、中国本地语言环境创建时间
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Asia/Hong_Kong"), Locale.CHINA);
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DATE, date);
        cal.set(Calendar.HOUR, hour);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.SECOND, second);
        cal.set(Calendar.MILLISECOND, millisecond);
        return cal.getTime();
    }

    /** 时间戳转日期 */
    public static String formattertime(String now) {
        Long timestamp = Long.parseLong(now) * 1000;
        String date = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(timestamp));
        return date;
    }

    /** 验证两个时间戳是否是同一天 */
    public static boolean checkSameDay(long time1, long time2) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        if (sf.format(new Date(time1 * 1000)).equals(sf.format(new Date(time2 * 1000)))) {
            return true;
        } else {
            return false;
        }
    }

    /** 验证两个时间戳是否是同一年 */
    public static boolean checkSameYear(long oldtime, long newtime) {
        if((Math.abs(Integer.valueOf(getYearByTime(newtime))-Integer.valueOf(getYearByTime(oldtime))))>0){
            return false;
        }else{
            return true;
        }
    }

    /** 验证两个时间戳是否是同一月 */
    public static boolean checkSameMonth(long oldtime, long newtime) {
        if((Math.abs(Integer.valueOf(getMonthTime(newtime))-Integer.valueOf(getMonthTime(oldtime))))>0){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 日期转时间戳 格式:yyyy::MM:dd HH:mm:ss
     * */
    public static long getTimestamp(String date) {
        long l = 0;
        if (!TextUtils.isEmpty(date)) {
            try {
                Date d1 = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss").parse(date);
                l = d1.getTime() / 1000;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return l;

    }

    /**
     * 计算星座
     *
     * @param month
     * @param day
     * @return
     */
    public static String getConstellation(Integer month, Integer day) {
        String s = "魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
        Integer[] arr = { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22 };
        Integer num = month * 2 - (day < arr[month - 1] ? 2 : 0);
        return s.substring(num, num + 2);
    }

    /**
     * 取得时间戳 精确到毫秒
     *
     * @return time
     */
    public static long getdatetime() {
        Date date = new Date();
        long time = date.getTime();
        return time;
    }

    /**
     * 获得唯一标识UUID
     */
    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    /***
     * 当天24点时间
     *
     * @return
     */
    public static int getTimesnight() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (int) (cal.getTimeInMillis() / 1000);
    }

    /**
     * 获取下一天0点时间
     *
     * @return
     */
    public static long getNextDate(long time) {
        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        String temp = df.format(new Date(time * 1000));
        long res = date2long(temp, "yyyy/MM/dd");
        res = res / 1000;
        res += (24 * 60 * 60);
        return res;
    }

    public static long date2long(String datestr, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date date = sdf.parse(datestr);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String getGroupTime(long timestamp){
        if(checkSameDay(System.currentTimeMillis()/1000, timestamp)){
            return "今天";
        }else if(converTime(timestamp).indexOf("昨天")>-1){
            return "昨天";
        }else{
            return getMonthTime(timestamp)+"月"+getDayTime(timestamp)+"日";
        }
    }

    public static long getBetweenDay(long start,long end){
        Date startDate = new Date(start*1000L);
        Date endTime = new Date(end*1000L);
        return (endTime.getTime()-startDate.getTime()) /  (1000L*3600L*24L);
    }

    /**
     * 通过时间戳获得年月日
     *
     * @param timestamp
     * @return
     */
    public static String getYYMMDDHHMM(Long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm");
        Date date = new Date(timestamp * 1000);
        return sdf.format(date);
    }

}
